home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 02 / 8 / DISK0283.ZIP / FFIND.C < prev    next >
C/C++ Source or Header  |  1985-01-23  |  2KB  |  94 lines

  1. /* Fast find for searching large source for a string - John N. White */
  2. /* The first arg is the string, any additional args must be on the same line
  3.  * (of the source file) in the correct order for the line to be printed.
  4.  * The names of files to be searched are read from stdin untill EOF */
  5. #define stdin 0        /* DeSmet value for stdin */
  6. int f,curflag,fs,fe;
  7. char *fname,fbuf[2048],line[258],curfile[128];
  8. char *le,*lend= &line[256];
  9.  
  10. main(argc,argv)
  11. char **argv;{
  12.     int i,j;
  13.     if(argc<2){
  14.         puts("need string to search for");
  15.         exit(1);
  16.     }
  17.     findall(argc,argv);
  18. }
  19.  
  20. /* find next file and set f to it */
  21. nextfile(){
  22.     int c,i;
  23.     if(f>0) close(f);
  24.     curflag=1;
  25.     fe=fs=2048;
  26. loop:
  27.     while((c=getc(stdin))<=' ' || c==',') if(c<0) exit(0);
  28.     i=0;
  29.     do{ if(i<127) curfile[i++]=c; }
  30.         while((c=getc(stdin))>' ' && c!=',');
  31.     curfile[i]=0;
  32.     f=open(curfile,0);
  33.     if(f<=0){
  34.         puts("******** can't open file ");
  35.         puts(curfile);
  36.         puts("\n");
  37.         goto loop;
  38.     }
  39. }
  40.  
  41. /* find all lines in the current file containing string */
  42. findall(argc,argv)
  43. char **argv;{
  44.     char *p,*q,*r,*string;
  45.     int c;
  46.     string=argv[1];
  47.     c= *string;
  48. loop:
  49.     getline();
  50.     for(p=line;p<le;p++) if(*p==c){
  51.         for(q=p,r=string;*r && *r== *q;q++,r++);
  52.         if(!*r && cmpnext(argc,argv,q)){
  53.             if(curflag){
  54.                 puts("---- file: ");
  55.                 puts(curfile);
  56.                 puts(" ----\n");
  57.                 curflag=0;
  58.             }
  59.             puts(line);
  60.             puts("\n");
  61.             goto loop;
  62.         }
  63.     }
  64.     goto loop;
  65. }
  66.  
  67. /* compare next arg with remainder of string, ret true if ok */
  68. cmpnext(argc,argv,cline)
  69. char **argv, *cline;{
  70.     char *p,*q,*r,*string;
  71.     int c;
  72.     if(argc<3) return(1);    /* if no more args */
  73.     string=argv[2];
  74.     c= *string;
  75.     for(p=cline;p<le;p++) if(*p==c){
  76.         for(q=p,r=string;*r && *r== *q;q++,r++);
  77.         if(!*r && cmpnext(argc-1,argv+1,q)) return(1);
  78.     }
  79.     return(0);
  80. }
  81.  
  82. /* get the next line of the current file into line */
  83. getline(){
  84.     int j;
  85. loop:
  86.     if(fe<1) nextfile();
  87.     for(le=line;
  88.         (j=fs<fe?fbuf[fs++]:((fs=1,(fe=read(f,fbuf,2048))<1)?'\n':*fbuf))!='\n'
  89.         && le<lend;
  90.         le++) *le=j;
  91.     *le=0;
  92.     if(! *line) goto loop;
  93. }
  94.